﻿/*
 * RegExpress
 * 
 * Copyright (c) 2010, Daniel McGaughran
 * 
 * Licensed under the Apache Licence, Version 2.0 (the "Licence");
 * you may not use this file except in compliance with the Licence.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Licence is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Licence for the specific language governing permissions and
 * limitations under the Licence.
 * 
 */

using System;
using System.Windows;
using RegExpressModel;
using RegExpressViewModel.Commands;
using XrControls.Util;
using XrControls.Entities;


namespace RegExpressViewModel
{
	public partial class MainWindowViewModel
	{
		private const string _pageUriTemplate = "{0}.xaml";
		private FrameworkElement _previousElement;


		protected MainWindowModel _model;
		protected WindowSettings _winSettings;

		protected SettingsViewModel _settings;


		public MainWindowViewModel()
		{
			RecalculateWorkspaceDimensions();
			_model = new MainWindowModel((int)MaxWidth, (int)MaxHeight);

			_settings = new SettingsViewModel(this, _model.Settings);
			_winSettings = _model.WindowSettings;

			TemplateNavigator.SetDataContext = SetPageDataContext;
			CloseCmd = new ActionCommand(CleanupApplication);
			FirstSlide = new ActionCommand(GoToFirstSlide);
			NextSlide = new ActionCommand(HasNextSlide, GoToNextSlide);
			PrevSlide = new ActionCommand(HasPrevSlide, GoToPrevSlide);
			UpdateNavChange();

			//Add keyboard command mappings
			KeyCommandSet commandSet = new KeyCommandSet();
			commandSet["Escape"] = CloseCmd;
			commandSet["F10"] = CloseCmd;
			commandSet["Home"] = FirstSlide;
			commandSet["Left"] = PrevSlide;
			commandSet["PageUp"] = PrevSlide;
			commandSet["F4"] = PrevSlide;
			commandSet["Right"] = NextSlide;
			commandSet["Next"] = NextSlide;
			commandSet["F6"] = NextSlide;
			KeyCommands.AddCommandSet("Root", commandSet);
			Instance = this;
			Visibility = Visibility.Visible;
		}




		private void RecalculateWorkspaceDimensions()
		{
			Rect dimensions = SystemParameters.WorkArea;

			MaxWidth = dimensions.Width;
			MaxHeight = dimensions.Height;
		}


		/// <summary>
		/// Performs the cleanup when the application is being closed.
		/// </summary>
		public void CleanupApplication()
		{
			_model.SaveState((int)CurrentWidth, (int)CurrentHeight, _winSettings.IsMaximised);
			Application.Current.Shutdown();
		}


		private void GoToFirstSlide()
		{
			if (_model.GoToFirstSlide())
				UpdateNavChange();
		}


		private bool HasNextSlide()
		{
			return _model.HasNextSlide();
		}

		private void GoToNextSlide()
		{
			if (_model.GoToNextSlide())
				UpdateNavChange();
		}


		private bool HasPrevSlide()
		{
			return _model.HasPrevSlide();
		}

		private void GoToPrevSlide()
		{
			if (_model.GoToPrevSlide())
				UpdateNavChange();
		}


		private void UpdateNavChange()
		{
			CurrentSlide = SlideViewModelFactory.CreateFrom(_model.CurrentSlide);
			OnPropertyChanged("CurrentSlideUri");
			PrevSlide.RefreshCanExecute();
			NextSlide.RefreshCanExecute();

			if (_previousElement != null)
				SetPageDataContext(_previousElement);
		}


		private void SetPageDataContext(FrameworkElement target)
		{
			if (target != null)
			{
				target.DataContext = CurrentSlide;
				_previousElement = target;
			}
		}


	}
}
